package com.blog.wu.myblog.controller;

import com.blog.wu.myblog.base.BaseController;
import com.blog.wu.myblog.entity.CategoryInfo;
import com.blog.wu.myblog.service.CategoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Author wuyanshen
 * @Date 2018-09-25 下午1:04
 * @Discription 栏目controller
 */
@Controller
@RequestMapping("/back/category")
public class CateogryInfoController extends BaseController {

    @Autowired
    private CategoryInfoService categoryInfoService;

    @RequestMapping("index")
    public String index(Model model) {
        List<CategoryInfo> list = categoryInfoService.findList(new CategoryInfo());
        model.addAttribute("catelist", list);
        return "/back/category/category_list";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(CategoryInfo categoryInfo, Model model) {
        CategoryInfo cat = categoryInfoService.get(categoryInfo);
        model.addAttribute("categoryInfo", cat);
        return "/back/category/category_update";
    }

    @RequestMapping("update")
    public String update(CategoryInfo categoryInfo, Model model) {
        boolean mark = categoryInfoService.update(categoryInfo);
        if (mark) {
            model.addAttribute("info", "修改栏目成功");
        } else {
            model.addAttribute("info", "修改栏目失败");
        }
        return "/back/category/category_update";
    }

    @RequestMapping("add")
    public String add(CategoryInfo categoryInfo, Model model) {
        boolean mark = categoryInfoService.add(categoryInfo);
        if (mark) {
            model.addAttribute("info", "添加栏目成功");
        } else {
            model.addAttribute("info", "添加栏目失败");
        }
        return "back/category/category_info";
    }

    @RequestMapping("delete")
    public String delete(CategoryInfo categoryInfo, Model model) {
        boolean mark = categoryInfoService.deleteAll(categoryInfo);
        if (mark) {
            model.addAttribute("info", "删除栏目成功");
        } else {
            model.addAttribute("info", "删除栏目失败");
        }
        return "redirect:/back/category/index";
    }
}
